<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- 
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License. 
-->
<html>
<head>
    <link type="text/css" rel="stylesheet" href="https://struts.apache.org/css/default.css">
    <style type="text/css">
        .dp-highlighter {
            width:95% !important;
        }
    </style>
    <style type="text/css">
        .footer {
            background-image:      url('https://cwiki.apache.org/confluence/images/border/border_bottom.gif');
            background-repeat:     repeat-x;
            background-position:   left top;
            padding-top:           4px;
            color:                 #666;
        }
    </style>
    <link href='https://struts.apache.org/highlighter/style/shCoreStruts.css' rel='stylesheet' type='text/css' />
    <link href='https://struts.apache.org/highlighter/style/shThemeStruts.css' rel='stylesheet' type='text/css' />
    <script src='https://struts.apache.org/highlighter/js/shCore.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushPlain.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushXml.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJava.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJScript.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushGroovy.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushBash.js' type='text/javascript'></script>
    <script type="text/javascript">
        SyntaxHighlighter.defaults['toolbar'] = false;
        SyntaxHighlighter.all();
    </script>
    <script type="text/javascript" language="javascript">
        var hide = null;
        var show = null;
        var children = null;

        function init() {
            /* Search form initialization */
            var form = document.forms['search'];
            if (form != null) {
                form.elements['domains'].value = location.hostname;
                form.elements['sitesearch'].value = location.hostname;
            }

            /* Children initialization */
            hide = document.getElementById('hide');
            show = document.getElementById('show');
            children = document.all != null ?
                    document.all['children'] :
                    document.getElementById('children');
            if (children != null) {
                children.style.display = 'none';
                show.style.display = 'inline';
                hide.style.display = 'none';
            }
        }

        function showChildren() {
            children.style.display = 'block';
            show.style.display = 'none';
            hide.style.display = 'inline';
        }

        function hideChildren() {
            children.style.display = 'none';
            show.style.display = 'inline';
            hide.style.display = 'none';
        }
    </script>
    <title>Nutshell</title>
</head>
<body onload="init()">
<table border="0" cellpadding="2" cellspacing="0" width="100%">
    <tr class="topBar">
        <td align="left" valign="middle" class="topBarDiv" align="left" nowrap>
            &nbsp;<a href="home.html">Home</a>&nbsp;&gt;&nbsp;<a href="guides.html">Guides</a>&nbsp;&gt;&nbsp;<a href="core-developers-guide.html">Core Developers Guide</a>&nbsp;&gt;&nbsp;<a href="nutshell.html">Nutshell</a>
        </td>
        <td align="right" valign="middle" nowrap>
            <form name="search" action="https://www.google.com/search" method="get">
                <input type="hidden" name="ie" value="UTF-8" />
                <input type="hidden" name="oe" value="UTF-8" />
                <input type="hidden" name="domains" value="" />
                <input type="hidden" name="sitesearch" value="" />
                <input type="text" name="q" maxlength="255" value="" />
                <input type="submit" name="btnG" value="Google Search" />
            </form>
        </td>
    </tr>
</table>

<div id="PageContent">
    <div class="pageheader" style="padding: 6px 0px 0px 0px;">
        <!-- We'll enable this once we figure out how to access (and save) the logo resource -->
        <!--img src="/wiki/images/confluence_logo.gif" style="float: left; margin: 4px 4px 4px 10px;" border="0"-->
        <div style="margin: 0px 10px 0px 10px" class="smalltext">Apache Struts 2 Documentation</div>
        <div style="margin: 0px 10px 8px 10px"  class="pagetitle">Nutshell</div>

        <div class="greynavbar" align="right" style="padding: 2px 10px; margin: 0px;">
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=45368">
                <img src="https://cwiki.apache.org/confluence/images/icons/notep_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Edit Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=45368">Edit Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">
                <img src="https://cwiki.apache.org/confluence/images/icons/browse_space.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Browse Space"></a>
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">Browse Space</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=45368">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_page_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=45368">Add Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=45368">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_blogentry_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add News"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=45368">Add News</a>
        </div>
    </div>

    <div class="pagecontent">
        <div class="wiki-content">
            <div id="ConfluenceContent"><div class="confluence-information-macro confluence-information-macro-information"><span class="aui-icon aui-icon-small aui-iconfont-info confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p> The framework documentation is written for active web developers and assumes a working knowledge about how Java web applications are built. For more about the underlying nuts and bolts, see the <a shape="rect" class="external-link" href="http://struts.apache.org/primer.html">Key Technologies Primer</a>.</p></div></div>

<h2 id="Nutshell-ApacheStruts2ArchitectureinaNutshell">Apache Struts 2 Architecture in a Nutshell</h2>

<div class="table-wrap"><table class="confluenceTable"><tbody><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> <span class="confluence-embedded-file-wrapper"><img class="confluence-embedded-image" src="nutshell.data/struts2-arch.png"></span> </p></td><td colspan="1" rowspan="1" class="confluenceTd"><ol><li>The web browser requests a resource (/mypage.action, /reports/myreport.pdf, et cetera)</li><li>The Filter Dispatcher looks at the request and determines the appropriate Action</li><li>The Interceptors automatically apply common functionality to the request, like workflow, validation, and file upload handling</li><li>The Action method executes, usually storing and/or retrieving information from a database</li><li>The Result renders the output to the browser, be it HTML, images, PDF, or something else</li></ol>
</td></tr></tbody></table></div>


<h2 id="Nutshell-StrutsTagsinanutshell">Struts Tags in a nutshell</h2>

<p>The Struts Tags help you create rich web applications with a minimum of coding. Often, much of the coding effort in a web application goes into the pages. The Struts Tags reduce effort by reducing code.</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">
&lt;% User user = ... %&gt;
&lt;form action="Profile_update.action" method="post"&gt;
    &lt;table&gt;
        &lt;tr&gt;
            &lt;td align="right"&gt;&lt;label&gt;First name:&lt;/label&gt;&lt;/td&gt;
            &lt;td&gt;&lt;input type="text" name="user.firstname" value="&lt;%=user.getFirstname() %&gt; /&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;input type="radio" name="user.gender" value="0" id="user.gender0" 
                &lt;% if (user.getGender()==0) { %&gt; checked="checked" %&gt; } %&gt; /&gt;
            &lt;label for="user.gender0"&gt;Female&lt;/label&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
&lt;/form&gt;
...
</pre>
</div></div>

<p>Looking over the markup, it's easy to see why Java web development without the aid from a modern framework is hard!  So far, we've only coded two controls, and there are six more to go! Let's rewrite and finish the form using Struts Tags.</p>


<div class="table-wrap"><table class="confluenceTable"><tbody><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> <span class="confluence-embedded-file-wrapper"><img class="confluence-embedded-image" src="nutshell.data/nutshell.GIF"></span> </p></td><td colspan="1" rowspan="1" class="confluenceTd"> <div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">
&lt;s:actionerror/&gt;
&lt;s:form action="Profile_update" validate="true"&gt;
    &lt;s:textfield label="Username" name="username"/&gt;
    &lt;s:password label="Password" name="password"/&gt;
    &lt;s:password label="(Repeat) Password" name="password2"/&gt;
    &lt;s:textfield label="Full Name" name="fullName"/&gt;
    &lt;s:textfield label="From Address" name="fromAddress"/&gt;
    &lt;s:textfield label="Reply To Address" name="replyToAddress"/&gt;
    &lt;s:submit value="Save" name="Save"/&gt;
    &lt;s:submit action="Register_cancel" value="Cancel" name="Cancel"  onclick="form.onsubmit=null"/&gt;
&lt;/s:form&gt;
</pre>
</div></div> <div class="confluence-information-macro confluence-information-macro-tip"><span class="aui-icon aui-icon-small aui-iconfont-approve confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p> The Struts Tags also support validation and localization as first-class features. So not only is there less code, but there is <em>more</em> utility. </p>

<p>In about the same amount of code as two conventional controls, the Struts Tags can create an entire data-input form with eight controls. Not only is there less code, but the code is easier to read and maintain. </p></div></div></td></tr></tbody></table></div>


<h2 id="Nutshell-StrutsConfigurationinaNutshell">Struts Configuration in a Nutshell</h2>

<p>A web application uses a deployment descriptor to initialize resources like filters and listeners. The web deployment descriptor is formatted as a XML document and named <code>web.xml</code>. Struts can either initialize its resources by scanning your classes using Java packages declared in this <code>web.xml</code> file, or you can have full control over the configuration via a configuration file, named <code>struts.xml</code>. These resources include action mappings, to direct input to server-side Action classes, and result types, to select output pages. </p>

<p>Here's a typical configuration (<code>struts.xml</code>) for a login workflow:</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">
&lt;struts&gt;
    &lt;package name="default" extends="struts-default"&gt;
        &lt;action name="Logon" class="mailreader2.Logon"&gt;
            &lt;result name="input"&gt;/pages/Logon.jsp&lt;/result&gt;
            &lt;result name="cancel" type="redirectAction"&gt;Welcome&lt;/result&gt;
            &lt;result type="redirectAction"&gt;MainMenu&lt;/result&gt;
            &lt;result name="expired" type="chain"&gt;ChangePassword&lt;/result&gt;
        &lt;/action&gt;

        &lt;action name="Logoff" class="mailreader2.Logoff"&gt;
            &lt;result type="redirectAction"&gt;Welcome&lt;/result&gt;
        &lt;/action&gt;
    &lt;/package&gt;
&lt;/struts&gt;
</pre>
</div></div>

<p><img class="emoticon emoticon-light-on" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/lightbulb_on.png" data-emoticon-name="light-on" alt="(lightbulb)"> The framework provides general-purpose defaults, so we can start using Struts right away, "out of the box". Any factory defaults can be overridden in an application's configuration, as needed.</p>

<h2 id="Nutshell-StrutsMVCinaNutshell">Struts MVC in a Nutshell</h2>

<p>Struts is a <a shape="rect" class="external-link" href="http://struts.apache.org/primer.html#mvc">Model View Controller</a> framework. Struts provides Controller and View components, and integrates with other technologies to provide the Model. The framework's Controller acts as a bridge between the application's Model and the web View. </p>

<p>To make it easier to present dynamic data, the framework includes a library of markup tags. The tags interact with the framework's validation and internationalization features, to ensure that input is correct and output is localized. The tag library can be used with JSP, FreeMarker, or Velocity. Of course, other tag libraries, JSTL, and AJAX can also be used, with or without the Struts tags. JavaServer Faces components are also supported.</p>

<p>When a request is received, the Controller invokes an Action class. The Action class examines or updates the application's state by consulting the Model (or, preferably, an interface representing the Model). To transfer data between the Model and the View, properties can be placed on the Action class, or on a plain old JavaBean. </p>

<p>Most often, the Model is represented as a graph of JavaBean objects. The Model should do the "heavy lifting", and the Action will act as a "traffic cop" or adapter. The framework provides sophisticated, automatic type conversion to simplify transfering data between rich domain objects and text-only HTTP requests. </p>

<p>Struts is extensible. <em>Very</em> extensible. Every class deployed by the framework is based on an interface. We provide all the base classes an application may ever need, but if we missed something, it's easy to add your own. We provide the general-purpose framework, but you can still write <em>your</em> application <em>your</em> way.</p>

<h2 id="Nutshell-IsStrutsthebestchoiceforeveryproject?">Is Struts the best choice for every project?</h2>

<p>Apache Struts 2 helps you create an extensible development environment for enterprise-grade applications, based on industry standards and proven design patterns. If you need to write a very simple application, with a handful of pages, then you might consider a "Model 1" solution that uses only server pages.</p>

<p>But, if you are writing a more complicated application, with dozens of pages, that need to be maintained over time, then Struts can help. For more about whether Model 1 or MVC/Model 2 is right for you, see <a shape="rect" class="external-link" href="http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html" rel="nofollow">Understanding JavaServer Pages Model 2 architecture</a>. </p>

<h2 id="Nutshell-PlatformRequirements">Platform Requirements </h2>

<p>Struts 2 requires </p>

<ul><li>Servlet API 2.4</li><li>JSP API 2.0</li><li>Java 5</li></ul>


<p>For a full list of requirements, including dependencies used by optional plugins, see <a shape="rect" class="external-link" href="http://struts.apache.org/2.x/struts2-core/dependencies.html">Project Dependencies</a> </p>

<p><img class="emoticon emoticon-tick" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/check.png" data-emoticon-name="tick" alt="(tick)"> An alternate set of JARs for Java 4 are also available. See the "J4" distribution. </p>

<p></p>

<h2 id="Nutshell-Next:">Next: <a shape="rect" href="ajax.html">AJAX</a></h2></div>
        </div>

        
    </div>
</div>
<div class="footer">
    Generated by CXF SiteExporter
</div>
</body>
</html>
